package com.ssy.lingxi.product.controller.pc.platform;

import com.ssy.lingxi.common.constant.tree.Node;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.product.api.model.request.CategoryRequest;
import com.ssy.lingxi.product.api.model.request.SimpleIdRequest;
import com.ssy.lingxi.product.api.model.response.commodity.CategoryResponse;
import com.ssy.lingxi.product.entity.platform.Attribute;
import com.ssy.lingxi.product.entity.platform.AttributeValue;
import com.ssy.lingxi.product.entity.platform.Category;
import com.ssy.lingxi.product.service.platform.ICategoryService;
import org.modelmapper.TypeToken;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @menu: 平台后台--品类管理
 * 平台后台--品类管理
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/6/22
 */
@RestController
@RequestMapping("/product/platform")
public class CategoryController extends BaseController {

    @Resource
    private ICategoryService categoryService;

    /**
     * 查询品类树
     * @param rootNodeId 根节点的key,空代表查询全部
     * @param excludeType 排除品类类型 品类类型：1-实物商品、2-虚拟商品、3-服务商品、4-积分兑换商品
     * @return Wrapper<List<Node>>
     */
    @RequestMapping(value = "/getCategoryTree", method = RequestMethod.GET)
    public Wrapper<List<Node>> getCategoryTree(@RequestParam(value = "rootNodeId", required = false) Long rootNodeId,Integer excludeType) {
        return Wrapper.success(categoryService.getCategoryTree(rootNodeId,excludeType));
    }

    /**
     * 查询品类列表
     * @param pageVO 分页实体
     * @param name 品类名称
     * @return Wrapper<PageData<Category>>
     */
    @RequestMapping(value = "/getCategoryList", method = RequestMethod.GET)
    public Wrapper<PageData<CategoryResponse>> getCategoryList(PageVO pageVO, @RequestParam("name") String name) {
        Page<Category> result = categoryService.getCategoryList(pageVO, name);
        List<CategoryResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<CategoryResponse>>() {}.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 查询品类信息
     * @param id 品类id
     * @return Wrapper<?>
     */
    @RequestMapping(value = "/getCategory", method = RequestMethod.GET)
    public Wrapper<CategoryResponse> getCategory(@RequestParam("id") Long id) {
        Category category = categoryService.getCategory(id);
        if(category != null){
            return Wrapper.success(this.modelMapper.map(category, CategoryResponse.class));
        }else{
            return Wrapper.success(null);
        }
    }

    /**
     * 查询品类信息--包括品类、属性、属性值
     * @param id 品类id
     * @return
     */
    @RequestMapping(value = "/getCategoryById", method = RequestMethod.GET)
    public Wrapper<Category> getCategoryById(@RequestParam("id") Long id) {
        Category category = categoryService.getCategory(id);
        if(category != null){
            List<Attribute> attributeList = category.getAttributeList().stream().filter(Attribute::getIsEnable).collect(Collectors.toList());
            attributeList.forEach(attribute -> {
                List<AttributeValue> attributeValueList = attribute.getAttributeValueList().stream().filter(AttributeValue::getIsEnable).collect(Collectors.toList());
                attribute.setAttributeValueList(attributeValueList);
            });
            category.setAttributeList(attributeList);
            return Wrapper.success(category);
        }else{
            return null;
        }
    }

    /**
     * 新增/修改品类
     * @param categoryRequest 品类实体
     * @return
     */
    @RequestMapping(value = "/saveOrUpdateCategory", method = RequestMethod.POST)
    public Wrapper<String> saveOrUpdateCategory(@RequestBody CategoryRequest categoryRequest){
        return Wrapper.success(categoryService.saveOrUpdateCategory(this.modelMapper.map(categoryRequest, Category.class)));
    }

    /**
     * 删除品类
     * @param simpleIdRequest
     * @return
     */
    @RequestMapping(value = "deleteCategory", method = RequestMethod.POST)
    public Wrapper<String> deleteCategory(@RequestBody SimpleIdRequest simpleIdRequest){
        return Wrapper.success(categoryService.deleteCategory(simpleIdRequest.getId()));
    }
}
